package com.dxb.test.regular;

import org.testng.annotations.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ClassName：com.dxb.test.regular.PreSurvey <br>
 * Description：正则表达式之正向预查和负向预查 <br>
 *
 * @author 戴晓波 <br>
 * Date 2022/2/10/010 16:26 <br>
 * @version v1.0 <br>
 */
public class PreSurvey {
    public static String words = "Windows 1.03 and Windows 2.0 fisrt Released in 1985 and 1987 respectively." +
            "Windows 95 and Windows 98 are the successor." +
            "Then Windows 2000 and Windows Xp Windows appeared." +
            "Windows Vista is the Latest version of the family." +
            "I have 11 Windows Operator system computer. You have no windows system to play game" +
            "Windows ...";

    private void print(Pattern compile) {
        Matcher matcher = compile.matcher(words);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

    /**
     * 匹配 Windows 后面是数字版本号的Windows 和这些字符
     * Windows 1.03
     * Windows 2.0
     * Windows 95
     * Windows 98
     * Windows 2000
     * Windows ...
     */
    @Test
    public void test1() {
        Pattern compile = Pattern.compile("Windows (?:[\\d.]+)");
        print(compile);
    }

    /**
     * 正向肯定预查
     * 匹配 Windows 后面是数字版本号的Windows 但不包括后面的字符
     * Windows 1.03
     * Windows 2.0
     * Windows 95
     * Windows 98
     * Windows 2000
     * Windows ...
     */
    @Test
    public void test2() {
        Pattern compile = Pattern.compile("Windows (?=[\\d.]+)");
        print(compile);
    }

    /**
     * 正向否定预查
     * 匹配 Windows 后面是数字版本号的Windows 但不包括后面的字符
     * Windows Xp
     * Windows appeared
     * Windows Vista
     * Windows Operator
     */
    @Test
    public void test3() {
        Pattern compile = Pattern.compile("Windows (?![\\d.]+)");
        print(compile);
    }

    /**
     * 反向肯定预查
     * 匹配Windows前面是数字的Windows
     * 11 Windows
     */
    @Test
    public void test4() {
        Pattern compile = Pattern.compile("(?<=[\\d].) Windows");
        print(compile);
    }

    /**
     * 反向否定预查
     * 匹配Windows前面不是数字的Windows
     * and Windows 2.0
     * and Windows 98
     * Then Windows 2000
     * and Windows Xp
     * Xp Windows appeared
     */
    @Test
    public void test5() {
        Pattern compile = Pattern.compile("(?<![\\d].) Windows");
        print(compile);
    }
}
